var i;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.HttpMgr = undefined;
var GameConfig = require("AA_GameConfig");
var LogManager = require("AA_LogManager");
var SingletonManager = require("AA_SingletonManager");
var s = function (t) {
  function o() {
    return null !== t && t.apply(this, arguments) || this;
  }
  __extends(o, t);
  o.prototype.post = function (t, o, n, i, a) {
    undefined === i && (i = 3);
    if (a = a || GameConfig.default.appConfig.ServerUrl) {
      a += t;
      !function t(i) {
        var r = new XMLHttpRequest();
        r.onreadystatechange = function () {
          if (4 === r.readyState) {
            if (200 == r.status) {
              try {
                var e = JSON.parse(r.responseText);
                n(null, e);
              } catch (e) {
                n(e, null);
              }
            } else if (i > 0) {
              LogManager.LogMgr.debug("[HttpManager] retry post request, tries left: " + i);
              t(i - 1);
            } else {
              LogManager.LogMgr.error("[HttpManager] request failed after retries with status: " + r.status);
              n("Request failed after retries with status: " + r.status, null);
            }
          }
        };
        r.open("POST", a, true);
        "object" == typeof o && r.setRequestHeader("Content-Type", "application/json");
        var s = "string" == typeof o ? o : JSON.stringify(o);
        r.send(s);
      }(i);
    } else {
      LogManager.LogMgr.error("[HttpManager] post error, url is none");
    }
  };
  o.prototype.requestData = function (e, t) {
    var o = cc.loader.getXMLHttpRequest();
    o.onreadystatechange = function () {
      if (4 == o.readyState) {
        var t = o.responseText;
        if (o.status >= 200 && o.status < 400) {
          var n = {};
          try {
            n = JSON.parse(t);
          } catch (e) {
            console.error("json parse error ", t);
            n = t;
          }
          e && e(n);
        } else {
          console.warn("error ", t);
        }
      } else {
        console.log("caller state change  ", o);
      }
    };
    o.timeout = 1e4;
    o.ontimeout = function (e) {
      console.error("error ", e);
    };
    o.open("POST", t, true);
    o.timeout = 2e4;
    o.send();
  };
  return o;
}(SingletonManager.SingletonMgr);
exports.HttpMgr = s.getInstance();